/******************************************************************************

	COPYRIGHT(C) JONAS 'SORTIE' TERMANSEN 2010.

	This file is part of Maxsi Engine.

	Maxsi Engine is free software: you can redistribute it and/or modify it
	under the terms of the GNU Lesser General Public License as published by
	the Free Software Foundation, either version 3 of the License, or (at your
	option) any later version.

	Maxsi engine is distributed in the hope that it will be useful, but WITHOUT
	ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
	FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
	for more details.

	You should have received a copy of the GNU Lesser General Public License
	along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	MaxsiEngine.dll
	A general purpose C++ library for the Maxsi Technology Project.

	MaxsiWindowControls.h
	An object oriented interface to the glorious inner workings of the Maxsi
	Window System, which allows you to more easily deal with UI elements.

******************************************************************************/

#include "MaxsiEngine.h"

BeginMaxsiNamespace

//=============================================================================
//	MaxsiWindowControl
//=============================================================================

MaxsiWindowControl::MaxsiWindowControl()
{
	WindowSystem		=	NULL;
	ControlSystem		=	NULL;
	Window				=	NULL;
	WindowLeft			=	0;
	WindowTop			=	0;
	WindowWidth			=	0;
	WindowHeight		=	0;
	Parent				=	NULL;
	Text				=	NULL;
	TextFont			=	NULL;
}

MaxsiWindowControl::MaxsiWindowControl(MaxsiWindow* NewParent, MaxsiWindowSystem* NewWindowSystem, MaxsiControlSystem* NewControlSystem)
{
	WindowSystem		=	NewWindowSystem;
	ControlSystem		=	NewControlSystem;
	Window				=	NULL;
	WindowLeft			=	0;
	WindowTop			=	0;
	WindowWidth			=	0;
	WindowHeight		=	0;
	Parent				=	NewParent;
	Text				=	NULL;
	TextFont			=	NULL;
}

MaxsiWindowControl::~MaxsiWindowControl()
{
	IFDEL(Text);
	if ( ControlSystem && Window ) { ControlSystem->DeleteControl(Window); }
}

MESTR* MaxsiWindowControl::GetControlType()
{
	return _MESTR("MaxsiWindowControl");
}

MaxsiError MaxsiWindowControl::Initialize()
{
	return (Window != NULL) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_COULD_NOT_CREATE_WINDOW;
}

MaxsiError MaxsiWindowControl::Destroy()
{
	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowControl::Show()
{
	return (WindowSystem->ShowWindow(Window)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

MaxsiError MaxsiWindowControl::Hide()
{
	return (WindowSystem->HideWindow(Window)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

int MaxsiWindowControl::Left() { return WindowLeft; }
int MaxsiWindowControl::Top() { return WindowTop; }
int MaxsiWindowControl::Width() { return WindowWidth; }
int MaxsiWindowControl::Height() { return WindowHeight; }
int MaxsiWindowControl::Right() { return Left()+Width(); }
int MaxsiWindowControl::Bottom() { return Top()+Height(); }

MaxsiError MaxsiWindowControl::Move(int NewLeft, int NewTop, int NewWidth, int NewHeight)
{
	WindowLeft = NewLeft; WindowTop = NewTop; WindowWidth = NewWidth; WindowHeight = NewHeight;

	return WindowSystem->MoveWindowControl(Parent->GetControlParent(), Window, NewLeft, NewTop, NewWidth, NewHeight, true);
}

MaxsiError MaxsiWindowControl::SetText(MESTR* NewText)
{
	MESTR* TextCopy = BuildString(1, NewText);

	if ( TextCopy == NULL ) { return MAXSI_ERROR_MEM_ALLOC_FAIL; }

	if ( Text ) { delete[] Text; }

	Text	=	TextCopy;

	return MAXSI_ERROR_SUCCESS;
}

MaxsiError MaxsiWindowControl::SetFont(MaxsiHandle NewFont)
{
	TextFont	=	NewFont;

	return MAXSI_ERROR_SUCCESS;
}

MESTR* MaxsiWindowControl::GetText()
{
	return BuildString(1, Text);
}

//=============================================================================
//	MaxsiWindowButton
//=============================================================================

MaxsiWindowButton::MaxsiWindowButton()
{
	
}

MaxsiWindowButton::MaxsiWindowButton(MaxsiWindow* NewParent, MaxsiWindowSystem* NewWindowSystem, MaxsiControlSystem* NewControlSystem)
{
	WindowSystem	=	NewWindowSystem;
	ControlSystem	=	NewControlSystem;
	Parent			=	NewParent;
	
	Window	=	ControlSystem->CreateButton(Parent->Window);
}

MaxsiWindowButton::~MaxsiWindowButton()
{

}

MESTR* MaxsiWindowButton::GetControlType()
{
	return _MESTR("MaxsiWindowButton");
}

MaxsiError MaxsiWindowButton::SetText(MESTR* NewText)
{
	MaxsiError	Result	=	BaseClass::SetText(NewText);

	if ( Result != MAXSI_ERROR_SUCCESS ) { return Result; }

	return (ControlSystem->SetButtonText(Window, Text)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

MaxsiError MaxsiWindowButton::SetFont(MaxsiHandle NewFont)
{
	return BaseClass::SetFont(NewFont);
}

//=============================================================================
//	MaxsiWindowLabel
//=============================================================================

MaxsiWindowLabel::MaxsiWindowLabel()
{
	
}

MaxsiWindowLabel::MaxsiWindowLabel(MaxsiWindow* NewParent, MaxsiWindowSystem* NewWindowSystem, MaxsiControlSystem* NewControlSystem)
{
	WindowSystem	=	NewWindowSystem;
	ControlSystem	=	NewControlSystem;
	Parent			=	NewParent;
	
	Window	=	ControlSystem->CreateLabel(Parent->Window);
}

MaxsiWindowLabel::~MaxsiWindowLabel()
{

}

MESTR* MaxsiWindowLabel::GetControlType()
{
	return _MESTR("MaxsiWindowLabel");
}

MaxsiError MaxsiWindowLabel::SetText(MESTR* NewText)
{
	MaxsiError	Result	=	BaseClass::SetText(NewText);

	if ( Result != MAXSI_ERROR_SUCCESS ) { return Result; }

	return (ControlSystem->SetLabelText(Window, Text)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

MaxsiError MaxsiWindowLabel::SetFont(MaxsiHandle NewFont)
{
	MaxsiError	Result	=	BaseClass::SetFont(NewFont);

	if ( Result != MAXSI_ERROR_SUCCESS ) { return Result; }

	return (ControlSystem->SetLabelFont(Window, NewFont)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

//=============================================================================
//	MaxsiWindowTextSingleLine
//=============================================================================

MaxsiWindowTextSingleLine::MaxsiWindowTextSingleLine()
{
	
}

MaxsiWindowTextSingleLine::MaxsiWindowTextSingleLine(MaxsiWindow* NewParent, MaxsiWindowSystem* NewWindowSystem, MaxsiControlSystem* NewControlSystem)
{
	WindowSystem	=	NewWindowSystem;
	ControlSystem	=	NewControlSystem;
	Parent			=	NewParent;
	
	Window	=	ControlSystem->CreateTextSingleLine(Parent->Window);
}

MaxsiWindowTextSingleLine::~MaxsiWindowTextSingleLine()
{

}

MESTR* MaxsiWindowTextSingleLine::GetControlType()
{
	return _MESTR("MaxsiWindowTextSingleLine");
}

MaxsiError MaxsiWindowTextSingleLine::SetText(MESTR* NewText)
{
	MaxsiError	Result	=	BaseClass::SetText(NewText);

	if ( Result != MAXSI_ERROR_SUCCESS ) { return Result; }

	return MAXSI_ERROR_NOT_IMPLEMENTED;

	//return (ControlSystem->SetLabelText(Window, Text)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

MaxsiError MaxsiWindowTextSingleLine::SetFont(MaxsiHandle NewFont)
{
	return BaseClass::SetFont(NewFont);
}

MESTR* MaxsiWindowTextSingleLine::GetText()
{
	return BaseClass::GetText();
}

//=============================================================================
//	MaxsiWindowProgressBar
//=============================================================================

MaxsiWindowProgressBar::MaxsiWindowProgressBar()
{
	
}

MaxsiWindowProgressBar::MaxsiWindowProgressBar(MaxsiWindow* NewParent, MaxsiWindowSystem* NewWindowSystem, MaxsiControlSystem* NewControlSystem)
{
	WindowSystem	=	NewWindowSystem;
	ControlSystem	=	NewControlSystem;
	Parent			=	NewParent;
	
	Window	=	ControlSystem->CreateProgressBar(Parent->Window);
}

MaxsiWindowProgressBar::~MaxsiWindowProgressBar()
{

}

MESTR* MaxsiWindowProgressBar::GetControlType()
{
	return _MESTR("MaxsiWindowProgressBar");
}

MaxsiError MaxsiWindowProgressBar::SetProgress(float Progress, size_t Type)
{
	return (ControlSystem->SetProgress(Window, Progress, Type)) ? MAXSI_ERROR_SUCCESS : MAXSI_ERROR_UNSPECIFIED;
}

EndMaxsiNamespace
